<?php

namespace App\Http\Controllers\admin;

use App\Http\Model\Links;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class LinksController extends CommonController
{

     //get.admin/links  全部友情链接列表
    public function index()
    {
        $data = Links::orderBy('link_order','asc')->get();
        return view('admin.links.index')->with('data',$data);
    }


     //get.admin/links/create   添加友情链接
    public function create()
    {
        return view('admin.links.add');;
    }


     //post.admin/links  添加友情链接提交
    public function store()
    {
        $input = Input::except('_token');
        $rules= [
            'link_name'=>'required',
        ];
        $message = [
            'link_name.required'=>'链接名称必须填写!'
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $row = Links::create($input);
            if($row){
                 return redirect('admin/links');
            }else{
                 return back()->with('errors','添加失败请稍后重试!');
            }
        }else{
            return back()->withErrors($validator);
        }
    }




    //get.admin/links/{links}/edit  编辑友情链接
    public function edit($link_id)
    {
        $row = Links::find($link_id);
        return view('admin.links.edit')->with('row',$row);
    }


     // put.admin/links/{links}    更新友情链接

    public function update($link_id)
    {
        $input = Input::except('_token','_method');
        $rules = ['link_name'=>'required', ];
        $message = ['link_name.required'=>'链接名字必须填写!'];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $model = Links::find($link_id);
            $row = $model->update($input);
            if($row){
                return redirect('admin/links')->with('errors','修改成功!');
            }else{
                return back()->with('errors','修改失败!');
            }
        }else{
            return back()->withErrors($validator);
        }
    }


   //delete.admin/links/{links}   删除友情链接
    public function destroy($link_id)
    {
        $row = Links::where('link_id',$link_id)->delete();
        if($row){
            $data = [
            'error'=>'0',
            'msg'=>'删除成功!'
            ];
        }else{
            $data = [
            'error'=>'1',
            'msg' =>'删除失败!'
            ];
        }
        return $data;
    }

    public function changOrder()
    {
        $input = Input::except('_token');
        $model = Links::find($input['link_id']);
        $row = $model->update(['link_order'=>$input['link_ord']]);
        if($row){
            $data = [
                'error'=>'0',
                'msg'=>'排序修改成功!'
            ];
        }else{
            $data = [
                'error'=>'1',
                'msg'=>'排序修改失败,请稍后重试!'
            ];
        }
        return $data;

    }

    //GET|HEAD | admin/link/{link}显示单个详情
    public function show($id)
    {
        //
    }
}
